//
//  PersonalCenterViewController.m
//  Mall
//
//  Created by 孙飞燕 on 16/9/28.
//  Copyright © 2016年 孙飞燕. All rights reserved.
//

#import "PersonalCenterViewController.h"

@interface PersonalCenterViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@property (nonatomic,strong)NSMutableArray *orderArr;//订单数据
@property (nonatomic,strong)NSMutableArray *setArr; //设置数据
@property (nonatomic,strong)UIImageView *headImage; //头像
@property (nonatomic,strong)PersonalDataViewController *dataVC;
@end

@implementation PersonalCenterViewController
- (NSMutableArray *)orderArr{
    if (!_orderArr) {
        _orderArr = [NSMutableArray arrayWithObjects:@"待付款",@"待发货",@"待收货",@"待评价",@"售后", nil];
    }
    return _orderArr;
}
- (NSMutableArray *)setArr{
    if (!_setArr) {
        _setArr = [NSMutableArray arrayWithObjects:@"我的收藏",@"设置",@"帮助与反馈", nil];
    }
    return _setArr;
}
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        _flowLayout = [[UICollectionViewFlowLayout alloc]init];
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        _flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:self.flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        //注册cell
        [_collectionView registerNib:[UINib nibWithNibName:@"PCCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PCCollectionViewCell"];
        [_collectionView registerNib:[UINib nibWithNibName:@"PCSecondCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PCSecondCollectionViewCell"];
        //注册头视图
        [_collectionView registerClass:[PersonalCenterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"collectionHeader"];
        [_collectionView registerClass:[PCCollectionReusableViewTitle class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderTitle"];
    }
    return _collectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }else if(section == 1){
        return 5;
    }else{
        return 3;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        PCCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PCCollectionViewCell" forIndexPath:indexPath];
        cell.cellLabel.text = self.orderArr[indexPath.row];
        return cell;
    }else if(indexPath.section == 2){
        PCSecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PCSecondCollectionViewCell" forIndexPath:indexPath];
        if (indexPath.row == 0) {
            cell.cellImage.image = [UIImage imageNamed:@"shoucang2"];
        }else if (indexPath.row == 1){
            cell.cellImage.image = [UIImage imageNamed:@"cogs-icon"];
        }else{
            cell.cellImage.image = [UIImage imageNamed:@"note"];
        }
        cell.cellLabel.text = self.setArr[indexPath.row];
        return cell;
    }else{
        return nil;
    }

}
//头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        if (indexPath.section == 0) {
            PersonalCenterCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"collectionHeader" forIndexPath:indexPath];
            
            UIView *view = [[UIView alloc]initWithFrame:headerView.frame];
            view.backgroundColor = [UIColor redColor];

            _headImage = [[UIImageView alloc]initWithFrame:CGRectMake(50, 10, 100, 100)];
            _headImage.center = headerView.center;
            _headImage.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.8];
            _headImage.layer.cornerRadius = 50;
            _headImage.layer.masksToBounds=YES;
            _headImage.layer.borderWidth = 2;
            _headImage.layer.borderColor = [UIColor lightGrayColor].CGColor;
            _headImage.userInteractionEnabled = YES;

            //给头像添加轻拍手势
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(alertChooseAction:)];
            [_headImage addGestureRecognizer:tap];
            [view addSubview:_headImage];

            [headerView addSubview:view];
            //创建登录按钮 (需要先将按钮添加到父视图上才能进行约束布局)
            UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeSystem];
            loginBtn.frame = CGRectMake(200, 100, 100, 50);
            [loginBtn addTarget:self action:@selector(loginBtnClickAction:) forControlEvents:UIControlEventTouchUpInside];
            [loginBtn setTitle:@"点击登录" forState:UIControlStateNormal];
            [view addSubview:loginBtn];
            [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.headImage.mas_right).with.offset(10);
                make.bottom.equalTo(self.headImage.mas_bottom);
            }];
            _headImage = _dataVC.headImage;

            return headerView;
        }else if(indexPath.section == 1){
            PCCollectionReusableViewTitle *titleView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderTitle" forIndexPath:indexPath];
            titleView.backgroundColor = [UIColor whiteColor];
            titleView.titleLabel.text = @"我的订单";

            [titleView.allOrderBtn addTarget:self action:@selector(allOrderBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            return titleView;
        }else{
            return nil;
        }
    }
        return nil;

}
//登录按钮的点击方法
- (void)loginBtnClickAction:(UIButton *)sender{
    LoginViewController *loginVC = [[LoginViewController alloc]init];
    [self.navigationController pushViewController:loginVC animated:YES];
}
//全部订单按钮的代理方法
- (void)allOrderBtnAction:(UIButton *)sender{
    OrderDetailViewController *detailVC = [[OrderDetailViewController alloc]init];
    [self.navigationController pushViewController:detailVC animated:YES];
}
//cell大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        return CGSizeMake(kWidth/5, 80);

    }else if (indexPath.section == 2){
        return CGSizeMake(kWidth,50);
    }else{
        return CGSizeMake(0,0);
    }
}
//头视图大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeMake(kWidth, 150);
    }else if (section == 1){
        return CGSizeMake(kWidth, 50);
    }
    else{
        return CGSizeMake(0,0);
    }

}
//点击cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 2) {
        if (indexPath.row == 1) {
            SettingViewController *settingVC = [[SettingViewController alloc]init];
            [self.navigationController pushViewController:settingVC animated:YES];
        }else if (indexPath.row == 0){
            CollectionViewController *collectionVC = [[CollectionViewController alloc]init];
            [self.navigationController pushViewController:collectionVC animated:YES];
        }else{
            HelpViewController *helpVC = [[HelpViewController alloc]init];
            [self.navigationController pushViewController:helpVC animated:YES];

        }

    }else{
        if (indexPath.row == 4) {
            HelpViewController *helpVC = [[HelpViewController alloc]init];
            [self.navigationController pushViewController:helpVC animated:YES];
        }else{
        OrderDetailViewController *detailVC = [[OrderDetailViewController alloc]init];
        [self.navigationController pushViewController:detailVC animated:YES];
        }
    }
}

//轻拍手势的回调方法
- (void)alertChooseAction:(UITapGestureRecognizer *)tap{
    //初始化弹出框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    //按钮：从相册选择
    [alert addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"%@",[NSThread currentThread]);
        //初始化UIImagePickerController
        UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
        //获取方式
        pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        //允许编辑
        pickerController.allowsEditing = YES;
        //设置代理
        pickerController.delegate = self;
        //页面跳转
        [self presentViewController:pickerController animated:YES completion:nil];
    }]];

    //按钮：拍照
    [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //初始化UIImagePickerController
        UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
        pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        pickerController.allowsEditing = YES;
        pickerController.delegate = self;
        [self presentViewController:pickerController animated:YES completion:nil];
    }]];

    //按钮：取消
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];

    [self presentViewController:alert animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

    [picker dismissViewControllerAnimated:YES completion:^{

    }];

}
//pickerImage完成后的代理方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{

//    //通过UIImagePickerControllerMediaType判断返回的是照片还是视频
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
//     //如果返回的type等于kUTTypeImage，代表返回的是照片,并且需要判断当前相机使用的sourcetype是拍照还是相册

    if ([type isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *theImage = nil;
        //判断照片是否允许修改
        if ([picker allowsEditing]) {
            //获取用户编辑之后的图像
            theImage = [info objectForKey:UIImagePickerControllerEditedImage];
            _headImage.image = theImage;
        }else{
            //照片的元数据参数
            theImage = [info objectForKey:UIImagePickerControllerOriginalImage];
            _headImage.image = theImage;
        }


    [picker dismissViewControllerAnimated:YES completion:nil];
}



}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
